package college.service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import college.dao.Attendance;
import college.dao.AttendanceDAO;
import college.dao.AttendanceId;
import college.dao.Branch;
import college.dao.BranchDAO;
import college.dao.ContactDetails;
import college.dao.ContactDetailsDAO;
import college.dao.Student;
import college.dao.StudentDAO;
import college.exception.SaveException;

@Service
public class CollegeService {
	
	
	@Autowired
	StudentDAO studentDAO;
	@Autowired
	AttendanceDAO attendanceDAO;
	@Autowired
	BranchDAO branchDAO;
	@Autowired
	ContactDetailsDAO contactDetailsDAO ;
	
	public void saveBranch(Branch branch)  {
		branch.setCode(branch.getCode().toUpperCase());
		try{
			branchDAO.save(branch);
		}catch (SaveException se) {
			throw se;
		}
		
	}
	public void saveStudent(Student student)  {
		student.setHtno(student.getHtno().toUpperCase());
		try{
			
			studentDAO.save(student);
		}catch (SaveException se) {
			throw se;
		}
		
	}
	public void saveContactDetails(ContactDetails contactDetails)  {
		contactDetails.getStudent().setHtno(contactDetails.getStudent().getHtno().toUpperCase());
		try{
			
			contactDetailsDAO.save(contactDetails);
		}catch (SaveException se) {
			throw se;
		}
		
	}
	public List<Branch> findAllBranches()  {
		return branchDAO.findAll();
		
	}
	public List<Student> findAllstudents()  {
		return studentDAO.findAll();
		
	}
	public List<Student> findAllstudents(String branchCode, String year, String sem)  {
		/*Student student = new Student();
		Branch branch = new Branch();
		branch.setCode(branchCode);
		student.setBranch(branch);
		student.setYear(Integer.parseInt(year));
		student.setSem(Integer.parseInt(sem));
		return studentDAO.findByExample(student);*/
		return studentDAO.findAllstudents(branchCode, Integer.parseInt(year), Integer.parseInt(sem));
		
	}
	public void saveAttendance(List<Attendance> attendanceList) {
		for(Attendance attendance: attendanceList ) {
			attendanceDAO.save(attendance);
		}
		 
	}
	
	public List<Attendance> findAttendance(String date, String hour)  {
		Attendance attendance = new Attendance();
		AttendanceId id = new AttendanceId();
		Date d=null;
		//date = "2012-09-06";
		String dateStrings[] = date.split("-");
		date = dateStrings[2] +"-"+dateStrings[1]+"-"+dateStrings[0];
		try {
			d = new SimpleDateFormat("yyyy-MM-dd").parse(date);
		//	d = DateFormat.getDateInstance().fo;
			
			id.setDate(d);
			id.setHour(Integer.parseInt(hour));
			attendance.setId(id);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return attendanceDAO.findByDateHour(d, Integer.parseInt(hour));
		
	}
	public List<Attendance> findAllAttendance()  {
		
		return attendanceDAO.findAll();
		
	}


}
